
// 参考 https://blog.csdn.net/m0_65121454/article/details/132383580

import {  createStore, combineReducers, applyMiddleware  } from "redux";
import { produce } from 'immer';


import counter from "./counter/index.js";
import user from "./user/index.js";

const rootReducer = combineReducers({
  counter,
  user
});

const immerMiddleware = store => next => action => {
  // 使用immer的produce函数生成新的state
  const newState = produce(store.getState(), draft => {
    /**
     * next可以调用对应的action里面的reducer
     * 并且可以拿到reducer返回的结果
     * 我们把返回的结果给draft赋值
     */
    draft = next(action);
  });
  // 返回新的state
  return newState;
};
 

export default createStore( rootReducer, applyMiddleware(immerMiddleware) );